<template>
  <div class="game-lobby">
    <!-- 背景特效 -->
    <div class="particles-background"></div>

    <!-- 主内容区域 -->
    <div class="content-container">
      <!-- 移动端顶部导航 -->
      <div class="mobile-header" v-if="isMobile">
        <el-button :icon="iconMenu" @click="toggleMenu" class="menu-btn"></el-button>
        <h2 class="app-title">游戏大厅</h2>
      </div>

      <!-- 主网格布局 -->
      <div class="grid-container">
        <!-- 用户信息区域（移动端侧边菜单） -->
        <div class="user-panel" :class="{'mobile-active': showMenu}">
          <UserProfile />
          <div class="mobile-menu-items" v-if="isMobile">
            <button @click="handleShowRoomPage" :class="{active: activePanel === 'roompage'}">
              <i class="icon-trophy"></i> 房间
            </button>
            <button @click="handleShowLeaderBoard" :class="{active: activePanel === 'leaderboard'}">
              <i class="icon-trophy"></i> 排行榜
            </button>
            <button @click=handleShowChat :class="{active: activePanel === 'chat'}">
              <i class="icon-chat"></i> 聊天室
            </button>
          </div>
        </div>

        <!-- 创建和加入房间区域（主内容） -->
        <div class="room-management-panel">
          <RoomManagement />
        </div>

        <!-- 聊天区域（桌面端显示） -->
        <div class="chat-panel" v-if="!isMobile">
          <ChatWindow />
        </div>

        <!-- 排行榜区域（移动端切换显示） -->
        <div class="mobile-panel" v-if="isMobile && activePanel === 'leaderboard'">
          <Leaderboard />
        </div>

        <!-- 聊天区域（移动端切换显示） -->
        <div class="mobile-panel" v-if="isMobile && activePanel === 'chat'">
          <ChatWindow />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import UserProfile from '../UserProfile.vue'
import ChatWindow from '../ChatWindow.vue'
import Leaderboard from '../Leaderboard.vue'
import RoomManagement from '../RoomManagement.vue'
import {Menu} from "@element-plus/icons-vue";

const showMenu = ref(false)
const activePanel = ref(null)
const windowWidth = ref(window.innerWidth)
const iconMenu = Menu

const isMobile = computed(() => windowWidth.value < 768)

const toggleMenu = () => {
  showMenu.value = !showMenu.value
}

const handleResize = () => {
  windowWidth.value = window.innerWidth
  if (windowWidth.value >= 768) {
    showMenu.value = false
  }
}

const handleShowLeaderBoard = () => {
  activePanel.value = 'leaderboard'
  showMenu.value = false
}

const handleShowChat = () => {
  activePanel.value = 'chat'
  showMenu.value = false
}

const handleShowRoomPage = () => {
  activePanel.value = 'roompage'
  showMenu.value = false
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.game-lobby {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

.particles-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  background-size: cover;
  opacity: 0.3;
}

.content-container {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 移动端顶部导航 */
.mobile-header {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  background: rgba(26, 26, 46, 0.9);
  backdrop-filter: blur(5px);
  z-index: 10;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.menu-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  margin-right: 15px;
  padding: 5px;
}

.app-title {
  color: white;
  font-size: 18px;
  margin: 0;
}

.grid-container {
  display: grid;
  grid-template-columns: 300px 1fr 300px;
  grid-template-rows: 100%;
  grid-template-areas: "user room chat";
  height: 100%;
  position: relative;
}

/* 用户面板/移动端菜单 */
.user-panel {
  grid-area: user;
  background: rgba(26, 26, 46, 0.8);
  backdrop-filter: blur(10px);
  border-right: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease;
}

/* 主内容区域 */
.room-management-panel {
  grid-area: room;
  background: rgba(22, 33, 62, 0.7);
  backdrop-filter: blur(10px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 桌面端聊天面板 */
.chat-panel {
  grid-area: chat;
  background: rgba(26, 26, 46, 0.8);
  backdrop-filter: blur(10px);
  border-left: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

/* 移动端面板 */
.mobile-panel {
  position: fixed;
  top: 60px;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(26, 26, 46, 0.95);
  backdrop-filter: blur(10px);
  z-index: 5;
  overflow: hidden;
  padding: 15px;
}

/* 移动端菜单项 */
.mobile-menu-items {
  padding: 15px;
}

.mobile-menu-items button {
  display: block;
  width: 100%;
  padding: 12px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 8px;
  color: white;
  text-align: left;
  font-size: 16px;
}

.mobile-menu-items button.active {
  background: rgba(52, 152, 219, 0.3);
}

.mobile-menu-items i {
  margin-right: 10px;
}

/* 移动端适配 */
@media (max-width: 767px) {
  .grid-container {
    grid-template-columns: 1fr;
    grid-template-rows: 100%;
    grid-template-areas: "room";
  }

  .user-panel {
    position: fixed;
    top: 60px;
    left: 0;
    bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    z-index: 20;
  }

  .user-panel.mobile-active {
    transform: translateX(0);
  }

  .chat-panel {
    display: none;
  }

  /* 遮罩层 */
  .menu-overlay {
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 15;
    display: none;
  }

  .menu-overlay.active {
    display: block;
  }
}

/* 平板适配 */
@media (min-width: 768px) and (max-width: 1023px) {
  .grid-container {
    grid-template-columns: 250px 1fr;
    grid-template-areas: "user room";
  }

  .chat-panel {
    display: none;
  }
}
</style>